/*
' Copyright (c) 2010 DotNetNuke Corporation
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
*/

using System;
using DotNetNuke.Services.Exceptions;
using System.Xml;
using System.Text;
using System.IO;

namespace DotNetNuke.Modules.DnnHeader
{

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The EditDnnHeader class is used to manage content
    /// </summary>
    /// -----------------------------------------------------------------------------
    public partial class Edit : DnnHeaderModuleBase
    {

        #region Event Handlers

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Page_Load runs when the control is loaded
        /// </summary>
        /// -----------------------------------------------------------------------------
        private void Page_Load(object sender, System.EventArgs e)
        {
            try
            {
                //Implement your edit logic for your module

            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }

            if (!IsPostBack)
            {
                string site = PortalAlias.HTTPAlias.ToString();
                int pid = PortalId;
                int mid = ModuleId;
                string urlpath = Server.HtmlEncode(Request.PhysicalApplicationPath);
                string portalpath = "Portals\\" + pid.ToString();
                string xpath = urlpath.ToString() + portalpath.ToString() + "\\DNNHeaderData-" + pid.ToString() + "-" + mid.ToString() + "-.xml";
                XmlDocument xdoc = new XmlDocument();
                xdoc.Load(xpath.ToString());
                string header = xdoc.GetElementsByTagName("Header")[0].InnerText;
                headertextbox.Text = header;
            }
        }

        #endregion

        protected void saveheader_Click(object sender, EventArgs e)
        {
            
                int pid = PortalId;
                int mid = ModuleId;
                string urlpath = Server.HtmlEncode(Request.PhysicalApplicationPath);
                string portalpath = "Portals\\" + pid.ToString();
                string xpath = urlpath.ToString() + portalpath.ToString() + "\\DNNHeaderData-" + pid.ToString() + "-" + mid.ToString() + "-.xml";
                XmlDocument xdoc = new XmlDocument();
                xdoc.Load(xpath);
                XmlNodeList nodelist = xdoc.SelectNodes("/DNNHeaderdb/HeaderDetails");
                nodelist[0].ChildNodes[0].InnerText = headertextbox.Text.ToString();

                xdoc.Save(xpath);
               
           
        }

    }

}